﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Text.RegularExpressions;
using System.ComponentModel;
using System.Diagnostics;

[assembly: WebResource("AjaxControlToolkit.WebControlLibrary.Images.update.gif", "img/gif")]
[assembly: WebResource("AjaxControlToolkit.WebControlLibrary.Images.updateh.gif", "img/gif")]
[assembly: WebResource("AjaxControlToolkit.WebControlLibrary.Label.js", "text/javascript")]

namespace AjaxControlToolkitCustom.LabelEditor
{
	[ValidationProperty("Text")]
	public class LabelEditor : System.Web.UI.WebControls.Label, IScriptControl
	{
		protected HiddenField htmlencodedText;
		protected HiddenField modified;
		protected HtmlGenericControl htmlEditor;
		protected HtmlGenericControl designEditor;
		protected HtmlGenericControl htmlArea;
		protected HtmlGenericControl designArea;
		protected HtmlGenericControl toolbarContainer;
		protected HtmlGenericControl htmlTab;
		protected HtmlGenericControl designTab;
		public Button btnEd;
		public TextBox txtEd;
		public Label lblEd;
		public ImageButton imgbtnEd;
		protected UpdatePanel updatePanel;
		protected bool updateHtml = false;
		protected ScriptManager sm;

		public LabelEditor()
			: base()
		{
		}

		public string lblEdID
		{
			get
			{
				if (ViewState["lblEdID"] != null)
					return (string)ViewState["lblEdID"];
				else
					return String.Empty;
			}
			set { ViewState["lblEdID"] = value; }
		}

		public int heighX
		{
			get
			{
				if (ViewState["heighX"] != null)
					return (int)ViewState["heighX"];
				else
					return 0;
			}
			set { ViewState["heighX"] = value; }
		}


		protected string GetWebResourceUrl(string resource)
		{
			return Page.ClientScript.GetWebResourceUrl(this.GetType(), resource);
		}

		protected HtmlGenericControl CreateEditorArea()
		{
			HtmlGenericControl area = new HtmlGenericControl("div");
			area.ID = "divIDX";
			area.Style["width"] = "140px";
			area.Style["height"] = "60px";
			area.Style["background-color"] = "#cccccc";

			lblEd = new Label();
			lblEd.Style["width"] = "140px";
			lblEd.Style["height"] = "15px";
			lblEd.Style["background-color"] = "pink";
			lblEd.Text = "Test";

			area.Controls.Add(lblEd);

			return area;
		}


		protected override void CreateChildControls()
		{
			this.Controls.Add(CreateEditorArea());

			base.CreateChildControls();
		}

		public override Unit Height
		{
			get
			{
				return heighX;
			}
			set
			{
				base.Height = heighX;
			}
		}

		protected override void OnPreRender(EventArgs e)
		{
			SetIDs();

			sm = ScriptManager.GetCurrent(Page);
			if (sm == null)
				throw new HttpException("A ScriptManager control must exist on the current page.");
			sm.RegisterScriptControl(this);

			base.OnPreRender(e);
		}

		protected void SetIDs()
		{
			lblEdID = "lblEdX";
			lblEd.ID = lblEdID;
		}

		protected override void Render(HtmlTextWriter writer)
		{
			sm.RegisterScriptDescriptors(this);

			base.Render(writer);
		}

		protected virtual IEnumerable<ScriptReference> GetScriptReferences()
		{
			ScriptReference htmlEditorReference = new ScriptReference("AjaxControlToolkit.WebControlLibrary.Label.js", "AjaxControlToolkit");

			return new ScriptReference[] { htmlEditorReference };
		}

		protected virtual IEnumerable<ScriptDescriptor> GetScriptDescriptors()
		{
			ScriptControlDescriptor descriptor = new ScriptControlDescriptor("AjaxControlToolkit.LabelEditor.LabelEditor", this.ClientID);
			descriptor.AddProperty("lblEdID", this.lblEd.ClientID);

			return new ScriptDescriptor[] { descriptor };
		}

		#region IScriptControl Members

		IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
		{
			return GetScriptDescriptors();
		}

		IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
		{
			return GetScriptReferences();
		}

		#endregion
	}
}
